package com.googlecode.maratische.google.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.html.HTML;
import javax.swing.text.html.HTML.Tag;

public final class StringUtils {

	private StringUtils() {
	}
	
	public static String replace(String text, String oldSub, String newSub) {
		while (text.indexOf(oldSub)>-1) {
			text = text.substring(0, text.indexOf(oldSub))+newSub+text.substring(text.indexOf(oldSub)+oldSub.length());
		}
		return text;
	}
	
	public static String getBetweenString(String text, String start, String end) {
		int i = 0;
		int j = 0;
		if ((i = text.indexOf(start)) > -1 && (j = text.indexOf(end, i + start.length())) > -1) {
			return text.substring(i + start.length(), j);
		}
		return null;
	}
	
	public static String removeWrongHtmlTags(String html) {
		Pattern pattern = Pattern.compile("\\<.*?>");
		Matcher matcher = pattern.matcher(html);
		while (matcher.find()) {
			String tagString = matcher.group();
			String tagName = tagString.substring(1,tagString.length()-1);
			if (tagName.length()>0) {
				tagName = (tagName.substring(0,1).equals("/")) ? tagName.substring(1) : tagName;
				tagName = (tagName.indexOf(" ")>0) ? tagName.substring(0, tagName.indexOf(" ")) : tagName;
				tagName = (tagName.substring(tagName.length()-1, tagName.length()).equals("/")) ? tagName.substring(0,tagName.length()-1) : tagName;
			}
			Tag tag = HTML.getTag(tagName.toLowerCase());
			if (tag == null) {
				html = html.replaceAll(tagString, "");
			}
		}
		return html;
	}

	/**
	 * Есть существенная проблема, что при отображении картинок, у которых указана только ширина, бьется рисунок :(
	 * @param text
	 * @return
	 */
	public static String fixWidthProblem(String text) {
		StringBuilder description2 = new StringBuilder();
		String description = text;
		if (description != null) {
			String temp = null;
			Pattern p1 = Pattern.compile(".*[ ]width[ =].*");
			Pattern p2 = Pattern.compile(".*[ ]height[ =].*");
			while ( (temp = getBetweenString(description, "<img ", ">")) != null) {
				temp = "<img "+temp+">";
				description2.append(getBeforeString(description, "<img "));
				description = getAfterString(description, "<img ", ">");
				int i =  0;
				boolean b1 = false;
				boolean b2 = false;
				if ( b1 = p1.matcher(temp).matches()) {
					i++;
				}
				if (b2 = p2.matcher(temp).matches()) {
					i++;
				}
				if (i==1) {
					//проблема!
					if (b1) {
//						logger.debug("remove width from "+temp);
						temp = temp.replaceFirst(" width", " ");
					}
					if (b2) {
//						logger.debug("remove height from "+temp);
						temp = temp.replaceFirst(" height", " ");
					}
				}
				description2.append(temp);
			}
			description2.append(description);
		}
		return description2.toString();
	}

	static public String removeXml(String str) {
		int sz = str.length();
		StringBuffer buffer = new StringBuffer(sz);
		boolean inTag = false;
		for (int i = 0; i < sz; i++) {
			char ch = str.charAt(i);
			if (ch == '<') {
				inTag = true;
			} else if (ch == '>') {
				inTag = false;
				continue;
			}
			if (!inTag) {
				buffer.append(ch);
			}
		}
		return buffer.toString();
	}

	public static String getAfterString(String text, String start, String end) {
		int i = 0;
		int j = 0;
		if ((i = text.indexOf(start)) > -1 && (j = text.indexOf(end, i + start.length())) > -1) {
			return text.substring(j + end.length());
		}
		return null;
	}

	public static String getBeforeString(String text, String start) {
		if (text.indexOf(start) > -1) {
			return text.substring(0, text.indexOf(start));
		}
		return text;
	}

}
